<?php

namespace backend\controllers;

use backend\models\Admin;
use common\models\ArticleBase;
use common\models\models\CategoryModel;
use common\models\models\ManagerModel;
use Yii;
use common\models\Category;

/**
 * ArticleController implements the CRUD actions for Article model.
 */
class ArticleController extends DashboardController
{
    #列表页面
    public function actionIndex()
    {
        $model = new ArticleBase();
        $data = $model->getAllArticle([]);
        if (!empty($data['items'])) {
            foreach ($data['items'] as &$item) {
                $item['admin'] = ManagerModel::uid2name($item['created_uid']);
                $item['cname'] = CategoryModel::cid2cname($item['category_id']);
            }
        }
        return $this->render('index',compact('data'));
    }

    # 添加文章
    public function actionAdd()
    {
        $category = Category::getAllCategory();
        return $this->render('add',compact('category'));
    }

    # 编辑文章
    public function actionEdit()
    {
        $id = intval(Yii::$app->request->get('id',0));
        if(empty($id)) {
            throw new \Exception("参数错误，非法请求");
        }
        $detail = ArticleBase::find()->where(['id' => $id])->asArray()->one();
        $category = Category::getAllCategory();

        return $this->render('edit',compact('detail','category'));
    }

    # 保存文章

    public function actionSave()
    {
        $post = Yii::$app->request->post();
        $item = empty($post['id']) ? new ArticleBase() : ArticleBase::find()->where(['id' => $post['id']])->one();
        $saveData = [
            'title' => !empty($post['title']) ? $post['title'] : '',
            'keywords' => !empty($post['keywords']) ? $post['keywords'] : '',
            'thumb' => !empty($post['thumb']) ? $post['thumb'] : '',
            'desc' => !empty($post['desc']) ? $post['desc'] : '',
            'source' => !empty($post['source']) ? $post['source'] : '',
            'content' => !empty($post['content']) ? $post['content'] : '',
            'category_id' => !empty($post['category_id']) ? $post['category_id'] : '',
            'created_uid' => Admin::uid(),
            'last_change' => time(),
            'status' => !empty($post['status']) ? $post['status'] : 0,
            'isdel' => !empty($post['isdel']) ? $post['isdel'] : 0
        ];

        $item->attributes = $saveData;

        if ($item->validate()){
            $msg = $item->save() ? ["state" => "success","message" => "恭喜小主，操作成功"] : ["state" => "error","message" => "小主大人，操作失败啦！"];
            $this->ajaxReturn($msg);
        } else {
            $this->ajaxReturn(["state" => 'error',"message" => '验证失败']);
        }

    }

    # 删除文章
    public function actionDelete()
    {
        $id = intval(Yii::$app->request->post('id',0));
        if (empty($id)) {
            $this->ajaxReturn(['state' => 'error','message' => "参数错误，非法操作"]);
        }
        $item = ArticleBase::find()->where(['id' => $id])->one();

        if($item){
            $transaction = Yii::$app->db->beginTransaction();
            if($item->delete()){
                $transaction->commit();
                $this->ajaxReturn([
                    'state' => 1,
                    'message' => '删除成功！',
                ]);
            }
            $transaction->rollBack();
            $this->ajaxReturn([
                'state' => 'error',
                'message' => '删除失败！'.implode('',array_flatten($item->getErrors())),
            ]);
        }else{
            $this->ajaxReturn([
                'state' => 'error',
                'message' => '数据不存在！',
            ]);
        }
    }
    # 切换文章状态


}
